package org.example.sdpback.controller;


import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.sdpback.domain.entity.Task;
import org.example.sdpback.domain.enums.TaskStatusEnum;
import org.example.sdpback.domain.enums.TaskTypeEnum;
import org.example.sdpback.domain.vo.TaskVO;
import org.example.sdpback.sdk.response.SdpResponseData;
import org.example.sdpback.service.ITaskService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxy
 * @since 2024-12-06
 */
@Tag(name = "任务管理")
@RestController
@RequestMapping("/task")
@RequiredArgsConstructor
public class TaskController {
    private final ITaskService taskService;

    @Operation(summary = "添加任务")
    @PostMapping("/add")
    public SdpResponseData<String> saveTask(@RequestBody Task task)
    {
        taskService.saveTask(task);
        return SdpResponseData.success();
    }

    @Operation(summary = "删除任务")
    @DeleteMapping("/delete/{taskId}")
    public SdpResponseData<String> deleteTask(@PathVariable Long taskId)
    {
        taskService.removeById(taskId);
        return SdpResponseData.success();
    }

    @Operation(summary = "修改任务")
    @PostMapping("/update")
    public SdpResponseData<String> updateTask(@RequestBody Task task)
    {
        task.setUpdateTime(LocalDateTime.now());
        taskService.updateById(task);
        return SdpResponseData.success();
    }

    @Operation(summary = "获取任务列表")
    @GetMapping("/list")
    public SdpResponseData<List<TaskVO>> listTask()
    {
        return SdpResponseData.success(taskService.taskList());
    }

    @Operation(summary = "获取任务类型状态")
    @GetMapping("/type/status")
    public SdpResponseData<JSONObject> getTaskTypeStatus()
    {
        List<String> typeValues = Arrays.stream(TaskTypeEnum.values()).map(taskTypeEnum -> taskTypeEnum.getName()).collect(Collectors.toList());
        List<String> statusValues = Arrays.stream(TaskStatusEnum.values()).map(taskStatusEnum -> taskStatusEnum.getName()).collect(Collectors.toList());
        JSONObject entries = new JSONObject();
        entries.put("taskType", typeValues);
        entries.put("taskStatus", statusValues);
        return SdpResponseData.success(entries);
    }

    @Operation(summary = "查询任务")
    @GetMapping("/list/search")
    @Parameters({
            @Parameter(name = "taskTitle", description = "任务名称", schema = @Schema(type = "string")),
            @Parameter(name = "username", description = "创建人"),
            @Parameter(name = "status", description = "任务状态"),
            @Parameter(name = "type", description = "任务类型")
    })
    public SdpResponseData<List<TaskVO>> searchListTask(@RequestParam(required = false) String taskTitle,
                                                        @RequestParam(required = false) String username,
                                                        @RequestParam(required = false) String status,
                                                        @RequestParam(required = false) String type
    ) {
        if (taskTitle == null && username == null)
        {
            return SdpResponseData.success(taskService.taskList());
        }
        List<TaskVO> taskVOS = taskService.taskList();
        List<TaskVO> collect = taskVOS.stream()
                .filter(taskVO -> taskVO.getTitle().contains(taskTitle == null ? "" : taskTitle))
                .filter(taskVO -> taskVO.getCreateUser().contains(username == null ? "" : username))
                .filter(taskVO -> taskVO.getStatus().contains(status == null ? "" : status))
                .filter(taskVO -> taskVO.getType().contains(type == null ? "" : type))
                .collect(Collectors.toList());
        return SdpResponseData.success(collect);
    }
}
